package xxm.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import xxm.dao.IArticleDao;
import xxm.exception.DAOException;
import xxm.model.Article;
import xxm.model.CateInfo;
import xxm.util.DB;


public class ArticleDaoImpl implements IArticleDao {

	private Connection con = null;
	private PreparedStatement ps=null;
	private ResultSet rs = null;
	
	@Override
	public boolean delete(Article e) throws DAOException {
		// TODO Auto-generated method stub
		//DELETE FROM xxm_marketing.tb_market_article_info WHERE market_article_id =
		String sql = "DELETE FROM xxm_marketing.tb_market_article_info WHERE market_article_id = ?";
		boolean bFlag = false;
		try {
			con = DB.getConnection();
			ps = DB.getPreparedStatement(con, sql);
			ps.setInt(1, e.getId());
			ps.executeUpdate();
			bFlag = true;
		} catch (Exception ex) {
			throw new DAOException(ex);
		}finally {
			DB.close(rs, ps, con);
		}
		return bFlag;
	}

	@Override
	public boolean delete(int id) throws DAOException {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM xxm_marketing.tb_market_article_info WHERE market_article_id = ?";
		boolean bFlag = false;
		try {
			con = DB.getConnection();
			ps = DB.getPreparedStatement(con, sql);
			ps.setInt(1, id);
			ps.executeUpdate();
			bFlag = true;
		} catch (Exception ex) {
			throw new DAOException(ex);
		}finally {
			DB.close(rs, ps, con);
		}
		return bFlag;
	}

	@Override
	public int getCount() throws DAOException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Article loadById(int id) throws DAOException {
		// TODO Auto-generated method stub
		
		String sql = "SELECT market_article_id, " +
			"market_cate_id, " +
			"market_article_title, " +
			"market_article_content, " +
			"market_article_original_site, " +
			"market_article_current_site, " +
			"market_article_short_site, " +
			"market_article_status, " +
			"market_article_date " +
			"FROM " +
			"xxm_marketing.tb_market_article_info WHERE market_article_id = ?";
		Article articel = null;
		try {
			con = DB.getConnection();
			ps = DB.getPreparedStatement(con, sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			while(rs.next()) {
				articel = new Article();
				articel.setId(rs.getInt("market_article_id"));
				articel.setCateId(rs.getInt("market_cate_id"));
				articel.setTitle(rs.getString("market_article_title"));
				articel.setContext(rs.getString("market_article_content"));
				articel.setOriginalSize(rs.getString("market_article_original_site"));
				articel.setCurrentSize(rs.getString("market_article_current_site"));
				articel.setShortSize(rs.getString("market_article_short_site"));
				articel.setStatus(rs.getInt("market_article_status"));
				articel.setDate(rs.getDate("market_article_date"));
			}
		} catch (Exception ex) {
			throw new DAOException(ex);
		}finally {
			DB.close(rs, ps, con);
		}
		return articel;
	}

	@Override
	public List<Article> paging(int pageSize, int pageCount, int rowCount)
			throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean save(Article e) throws DAOException {
		// TODO Auto-generated method stub
		if( null == e ) {
			return false;
		}
		String sql = "INSERT INTO xxm_marketing.tb_market_article_info "+
		"(market_cate_id, "+
		"market_article_title, "+
		"market_article_content, "+
		"market_article_original_site, "+
		"market_article_current_site, "+
		"market_article_short_site "+
		//"market_article_status, "+
		//"market_article_date"+
		")"+
		"VALUES"+
		"(?, "+
		"?,  "+
		"?, "+
		"?, "+
		"?, "+
		"? "+
		");";
		boolean bFlag = true;
		try {
			con = DB.getConnection();
			ps = DB.getPreparedStatement(con, sql);
			//ps.setInt(1, e.getId());
			ps.setInt(1, e.getCateId());
			ps.setString(2, e.getTitle());
			ps.setString(3, e.getContext());
			ps.setString(4, e.getOriginalSize());
			ps.setString(5, e.getCurrentSize());
			ps.setString(6, e.getShortSize());
			//ps.setInt(8, e.getStatus());
			//ps.setDate(9, e.getDate());
			
			
			if( ps.executeUpdate() ==1 ) {
				bFlag = true;
			}
		} catch (Exception ex) {
			throw new DAOException(ex);
		}finally {
			DB.close(rs, ps, con);
		}
		return bFlag;
	}

	@Override
	public boolean update(Article article) throws DAOException {
		// TODO Auto-generated method stub
		try {
			String sql = "UPDATE xxm_marketing.tb_market_article_info "+
			"SET "+
			"market_cate_id = ? , "+
			"market_article_title = ? , "+
			"market_article_content = ? , "+
			"market_article_original_site = ? , "+
			"market_article_current_site = ? , "+
			"market_article_short_site = ?,  "+
			"market_article_status = ?  " +
			//"market_article_date = ? "+
			
			"WHERE "+
			"market_article_id = ? ;";
			con = DB.getConnection();
			ps = DB.getPreparedStatement(con, sql);
			ps.setInt(1, article.getCateId());
			ps.setString(2, article.getTitle());
			ps.setString(3, article.getContext());
			ps.setString(4, article.getOriginalSize());
			ps.setString(5, article.getCurrentSize());
			ps.setString(6, article.getShortSize());
			ps.setInt(7, article.getStatus());
			//ps.setDate(8, article.getDate());
			
			ps.setInt(8, article.getId());
			int result = ps.executeUpdate();
			if (result == 1) {
				return true;
			}
		}catch( Exception ex) {
			throw new DAOException(ex);
		}
		return false;
	}

	@Override
	public List<Article> getTableArray() throws DAOException {
		// TODO Auto-generated method stub
		List<Article> ListArray = new ArrayList<Article>();
		try {
			String sql = "SELECT market_article_id, " +
			"market_cate_id, " +
			"market_article_title, " +
			"market_article_content, " +
			"market_article_original_site, " +
			"market_article_current_site, " +
			"market_article_short_site, " +
			"market_article_status, " +
			"market_article_date " +
			"FROM " +
			"xxm_marketing.tb_market_article_info";
			con = DB.getConnection();
			ps = DB.getPreparedStatement(con, sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Article articel = new Article();
				articel.setId(rs.getInt("market_article_id"));
				articel.setCateId(rs.getInt("market_cate_id"));
				articel.setTitle(rs.getString("market_article_title"));
				articel.setContext(rs.getString("market_article_content"));
				articel.setOriginalSize(rs.getString("market_article_original_site"));
				articel.setCurrentSize(rs.getString("market_article_current_site"));
				articel.setShortSize(rs.getString("market_article_short_site"));
				articel.setStatus(rs.getInt("market_article_status"));
				articel.setDate(rs.getDate("market_article_date"));
				ListArray.add(articel);
			}
		} catch( Exception e) {
			throw new DAOException(e);
		}
		return ListArray;
	}

	@Override
	public List<Article> getTableVectorByName(String strKey) throws DAOException {
		// TODO Auto-generated method stub
		List<Article> ListArray = new ArrayList<Article>();
		try {
			String sql = "SELECT market_article_id, " +
			"market_cate_id, " +
			"market_article_title, " +
			"market_article_content, " +
			"market_article_original_site, " +
			"market_article_current_site, " +
			"market_article_short_site, " +
			"market_article_status, " +
			"market_article_date " +
			"FROM " +
			"xxm_marketing.tb_market_article_info where market_article_title like ?";
			con = DB.getConnection();
			ps = DB.getPreparedStatement(con, sql);
			ps.setString(1, "%"+strKey+"%");
			rs = ps.executeQuery();
			while (rs.next()) {
				Article articel = new Article();
				articel.setId(rs.getInt("market_article_id"));
				articel.setCateId(rs.getInt("market_cate_id"));
				articel.setTitle(rs.getString("market_article_title"));
				articel.setContext(rs.getString("market_article_content"));
				articel.setOriginalSize(rs.getString("market_article_original_site"));
				articel.setCurrentSize(rs.getString("market_article_current_site"));
				articel.setShortSize(rs.getString("market_article_short_site"));
				articel.setStatus(rs.getInt("market_article_status"));
				articel.setDate(rs.getDate("market_article_date"));
				ListArray.add(articel);
			}
		} catch( Exception e) {
			throw new DAOException(e);
		}
		return ListArray;
	}

	@Override
	public List<Article> getTableVectorByCode(String strKey) throws DAOException {
		// TODO Auto-generated method stub
		List<Article> ListArray = new ArrayList<Article>();
		try {
			String sql = "SELECT market_article_id, " +
			"market_cate_id, " +
			"market_article_title, " +
			"market_article_content, " +
			"market_article_original_site, " +
			"market_article_current_site, " +
			"market_article_short_site, " +
			"market_article_status, " +
			"market_article_date " +
			"FROM " +
			"xxm_marketing.tb_market_article_info where market_article_id like ? "; //or market_cate_id like ?";
			con = DB.getConnection();
			ps = DB.getPreparedStatement(con, sql);
			ps.setString(1, "%"+strKey+"%");
			//ps.setString(2, "%"+strKey+"%");
			rs = ps.executeQuery();
			while (rs.next()) {
				Article articel = new Article();
				articel.setId(rs.getInt("market_article_id"));
				articel.setCateId(rs.getInt("market_cate_id"));
				articel.setTitle(rs.getString("market_article_title"));
				articel.setContext(rs.getString("market_article_content"));
				articel.setOriginalSize(rs.getString("market_article_original_site"));
				articel.setCurrentSize(rs.getString("market_article_current_site"));
				articel.setShortSize(rs.getString("market_article_short_site"));
				articel.setStatus(rs.getInt("market_article_status"));
				articel.setDate(rs.getDate("market_article_date"));
				ListArray.add(articel);
			}
		} catch( Exception e) {
			throw new DAOException(e);
		}
		return ListArray;
	}

}
